package com.carlife.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.carlife.provider.ConditionItem;
import com.carlife.provider.Conditions;
import com.carlife.provider.IModel;
import com.carlife.util.Constants;


public class MileageLog implements IModel {
	
	public static final String TABLENAME = "T_MILEAGE_LOG";
	
	public static final String FD_MILEAGE_NO = "MILEAGE_NO";
	public static final String FD_CAR_NO = "CAR_NO";
	public static final String FD_INPUT_TIME = "INPUT_TIME";
	public static final String FD_MILEAGE_VAlUE = "MILEAGE_VAlUE";
	
	public static final String [] FIELDS = new String [] {
		FD_MILEAGE_NO,
		FD_CAR_NO,
		FD_INPUT_TIME,
		FD_MILEAGE_VAlUE
	};
	
	public static final String [] PK_FIELDS = new String [] {
		FD_MILEAGE_NO
	};
	
	private ContentValues maps = new ContentValues(4);
	
	public MileageLog() {
		super();
	}
	
	public void put(String key, String value) {
		this.maps.put(key, value);
	}
	
	public Object get(String key) {
		return this.maps.get(key);
	}
	
	@Override
	public ContentValues getContentValues() {
		return this.maps;
	}
	@Override
	public String getModelName() {
		return TABLENAME;
	}
	@Override
	public Conditions getPKCondition() {
		Conditions cond = new Conditions();
		for (int i=0; i<PK_FIELDS.length; i++) {
			ConditionItem item = new ConditionItem(PK_FIELDS[i], maps.get(PK_FIELDS[i]), ConditionItem.EQ);
			cond.addItem(item);
		}
		return cond;
	}
	
	@Override
	public IModel pareseNewModel(Cursor cursor) {
		MileageLog model = new MileageLog();
		for (int i=0; i<FIELDS.length; i++) {
			int columnIndex = cursor.getColumnIndex(FIELDS[i]);
			Log.d(Constants.LOG_TAG, FIELDS[i] + ": index= " + columnIndex + ", value = " + cursor.getString(columnIndex));
			model.maps.put(FIELDS[i], cursor.getString(columnIndex));
		}
		return model;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		for (int i=0; i<FIELDS.length; i++) {
			buffer.append(FIELDS[i]).append("=").append(get(FIELDS[i])).append(";");
		}
		return buffer.toString();
	}
	
	
	
}
